#!/bin/bash
#Date 2022/1/10
#mail it-arch
echo "##########################################"
echo "Auto Install zabbix-proxy.              ##"
echo "Press Ctrl + C to cancel                ##"
echo "Any key to continue                     ##"
echo "##########################################"
echo "1 Install zabbix3.0-proxy"
echo "2 Install zabbix3.2-proxy"
echo "3 EXIT"
read -p "Input your choce:" I
if [ "$I" == "1" ];then
	URL=http://repo.zabbix.com/zabbix/3.0/rhel/6/x86_64/zabbix-release-3.0-1.el6.noarch.rpm
elif [ "$I" == "2" ];then
	URL=http://repo.zabbix.com/zabbix/3.2/rhel/6/x86_64/zabbix-release-3.2-1.el6.noarch.rpm
elif [ "$I" == "3" ];then
	echo "you choce channel!"
	exit 0;
else
	echo "input Error! Place input{1|2|3}"
	exit 1;
fi
PIP=`ifconfig |grep "inet addr" |grep -v "127.0.0.1" |awk -F: '{ print $2 }'| awk '{ print $1}'`
read -p "Place input zabbix server ipaddress:" SIP
/etc/init.d/iptables status >/dev/null 2>&1
if [ $? -eq 0 ]
then
iptables -I INPUT -p tcp --dport 10051 -j ACCEPT &&
iptables -I INPUT -p tcp --dport 10050 -j ACCEPT &&
/etc/init.d/iptables save >/dev/null 2>&1
else
	echo -e "\033[32m iptables is stopd\033[0m"
fi
sed -i "s/SELINUX=enforcing/SELINUX=disabled/"  /etc/selinux/config
setenforce 0
yum install -y ntpdate mailx dos2unix vim zcat wget net-snmp-utils
ntpdate -s time1.aliyun.com 
echo "*/5 * * * * /usr/sbin/ntpdate -s time1.aliyun.com">>/var/spool/cron/root
echo -e "\033[32m MySQL 安装... \033[0m"
yum -y install gcc gcc-c++ autoconf httpd libxml* mysql mysql-server  
###################################################
echo -e "\033[32m 创建zabbix账户 \033[0m"
groupadd zabbix 
useradd -g zabbix zabbix 
echo -e "\033[32m zabbix proxy服务安装 \033[0m"
#rpm  -ivh  http://repo.zabbix.com/zabbix/3.0/rhel/6/x86_64/zabbix-release-3.0-1.el6.noarch.rpm 
rpm -ivh  $URL
yum install zabbix-proxy-mysql* -y
#################################################
service mysqld start 
chkconfig mysqld on 
##############################################
echo -e "\033[32m 创建数据库，导入数据 \033[0m"
mysql -uroot -e "create database zabbix_proxy character set utf8;"
mysql -uroot -e "grant all on zabbix_proxy.* to zabbix@localhost identified by 'zabbix';"
mysql -uroot -e "flush privileges;"
cd /usr/share/doc
MYSQLDIR=`ls -l /usr/share/doc/ | grep zabbix-proxy-mysql* | awk  '{print $9}'`
cd $MYSQLDIR
zcat schema.sql.gz | mysql -uzabbix -pzabbix zabbix_proxy
sed -i "s/Server=127.0.0.1/Server=$SIP/g" /etc/zabbix/zabbix_proxy.conf
sed -i "s/Hostname=Zabbix\ proxy/Hostname=$PIP/g" /etc/zabbix/zabbix_proxy.conf
echo "DBPassword=zabbix">>/etc/zabbix/zabbix_proxy.conf
/etc/init.d/zabbix-proxy start
chkconfig zabbix-proxy on 
STATE=`ps -ef |grep zabbix_proxy|grep -v grep|wc -l`
PORT=`netstat -lntup|grep zabbix_proxy|wc -l`
if [ $STATE > 0 -a $PORT > 0 ];then
	echo -e "\033[32m zabbix-proxy is install success! \033[0m"
	echo -e "\033[32m zabbix-server IP:$SIP \033[0m"
	echo -e "\033[32m zabbix-proxy HOSTNAME:$PIP \033[0m"
	echo -e "\033[32m mysqluser:root mysqlpasswd:mysqladmin \033[0m"
else
        echo "\033[32m zabbix-proxy error! place see /var/log/zabbix/zabbix_proxy.log \033[0m"
fi

